package com.godme.idea;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SplitBag {
    static class Goods{
        double price;
        double residue;
        String name;

        public Goods(String name, double weight, double value){
            this.name = name;
            this.residue = weight;
            this.price = 1.0 * value / weight;
        }
        boolean isDeplete(){
            return residue == 0;
        }

        public double getPrice(){
            return price;
        }

        public void cost(double use){
            this.residue = this.residue - use;
        }
    }

    public static List<Goods> sort(Goods ...goods){
        return Arrays.stream(goods).sorted((g1, g2)->(int) (100 * (g2.price - g1.price))).collect(Collectors.toList());
    }

    public static double loads(double bagSize, List<Goods> goodsList, double value){
        if(bagSize <= 0 || goodsList.isEmpty()){
            return value;
        }
        Goods goods = goodsList.get(0);
        double load = Math.min(bagSize, goods.residue);
        goods.cost(load);
        if(goods.isDeplete()){
            goodsList.remove(0);
        }
        return loads(bagSize - load, goodsList, value + goods.price * load);
    }

    public static void main(String[] args) {
        Goods g1 = new Goods("A", 10, 60); // 6
        Goods g2 = new Goods("B", 20, 100);// 5
        Goods g3 = new Goods("C", 30, 120);// 4
        System.err.println(loads(50, sort(g1, g2, g3), 0));
    }








}
